﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Threading;
using System.Globalization;
using System.Data;

public partial class NewPublisher : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            DataSet ds = DB.getdata("Select * from publishers where id = " + id);
            if (ds.Tables[0].Rows.Count > 0)
            {
                country.DataBind();
                city.DataBind();

                DataRow dr = ds.Tables[0].Rows[0];
                companyname.Text = dr["publisher"].ToString();
                country.SelectedValue = dr["country_id"].ToString();
                city.SelectedValue = dr["city_id"].ToString();
                address.Text = dr["address"].ToString();
                postalcode.Text = dr["postalcode"].ToString();
                postalcode.Text = dr["postalcode"].ToString();
                string code = country.SelectedItem.Text.Split(',')[1];
                code1.Text = code;
                code2.Text = code;
                code3.Text = code;

                tele1.Text = dr["tele1"].ToString();
                tele2.Text = dr["tele2"].ToString();
                fax.Text = dr["fax"].ToString();
                email1.Text = dr["email1"].ToString();
                username.Text = dr["website"].ToString();
                if (dr["longitude"].ToString() != "")
                {


                    double longs = double.Parse(dr["longitude"].ToString());
                    double lats = double.Parse(dr["latitude"].ToString());
                    GooglePoint GP1 = new GooglePoint();

                    GP1.ID = dr["address"].ToString();
                    GP1.Latitude = lats;
                    GP1.Longitude = longs;

                    GP1.InfoHTML = dr["address"].ToString();

                    GP1.IconImage = "icons/pushpin-yellow.png";

                    GoogleMapForASPNet1.GoogleMapObject.CenterPoint = new GooglePoint("previous", lats + 0.05, longs - 0.05);
                    GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP1);

                }


            }
            else
            {
                throw new Exception("there is no publisher with this id ");
            }


        }
        else
        {

        }
    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                

            }
        }

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "reports", "there();", true);
        // div1.Visible = true;
        GoogleMapForASPNet1.setseen();
    }

    protected void country_SelectedIndexChanged(object sender, EventArgs e)
    {
       
        //24.68632852205518,46.71901702880859
        //24.67291413305010,46.74167633056641
        string code = country.SelectedItem.Text.Split(',')[1];
        code = code.Trim();
        code1.Text = code;
        code2.Text = code;
        code3.Text = code;
        //country.AutoPostBack = false;


    }
    protected void save_Click(object sender, EventArgs e)
    {

        if (Page.IsValid)
        {
            List<string> fields = new List<string>();
            ArrayList values = new ArrayList();
            List<string> types = new List<string>();
            ///////////////



            fields.Add("publisher");
            types.Add("string");
            values.Add(companyname.Text);
            ////////////////////////////////////////////////
            fields.Add("country_id");
            types.Add("string");
            values.Add(country.SelectedValue);
            ///////////////////////
            fields.Add("country");
            types.Add("string");
            values.Add(country.SelectedItem.Text);
            ///////////////////////
            fields.Add("city_id");
            types.Add("string");
            values.Add(city.SelectedValue);
            ///////////////////////
            fields.Add("city");
            types.Add("string");
            values.Add(city.SelectedItem.Text);
            ///////////////////////
            fields.Add("address");
            types.Add("string");
            values.Add(address.Text);
            ///////////////////////

            ///////////////////////
            fields.Add("postalcode");
            types.Add("string");
            values.Add(postalcode.Text);
            ///////////////////////
            fields.Add("tele1");
            types.Add("string");
            values.Add(tele1.Text);
            ///////////////////////
            fields.Add("tele2");
            types.Add("string");
            values.Add(tele2.Text);
            ///////////////////////
            fields.Add("fax");
            types.Add("string");
            values.Add( fax.Text);
            ///////////////////////
            fields.Add("email1");
            types.Add("string");
            values.Add(email1.Text);
            ///////////////////////
            ///////////////////////
            fields.Add("website");
            types.Add("string");
            values.Add(username.Text);
            ///////////////////////
            int countpoint = GoogleMapForASPNet1.GoogleMapObject.Points.Count;
            if (countpoint > 0)
            {
                ///////////////////////
                fields.Add("longitude");
                types.Add("decimal");
                values.Add(GoogleMapForASPNet1.GoogleMapObject.Points[countpoint-1].Longitude);
                ///////////////////////
                fields.Add("latitude");
                types.Add("decimal");
                values.Add(GoogleMapForASPNet1.GoogleMapObject.Points[countpoint-1].Latitude);
                ///////////////////////
            }
            ///////////////////////
            int f = (int)DB.UPDATE("publishers", "int,id," + Request.QueryString["id"], fields, types, values);
            

        }
    }
    protected void cancels(object sender, EventArgs e)
    {
        Response.Redirect("~/MainForm2.aspx");

    }
    
    
    
    protected void enter_Click(object sender, EventArgs e)
    {
         int countpoint =GoogleMapForASPNet1.GoogleMapObject.Points.Count ;
         if (countpoint > 0)
         {
             address.Text = GoogleMapForASPNet1.GoogleMapObject.Points[countpoint-1].InfoHTML;
         }
         
    }

   
}
